export const unitOptions = [
  { label: '元', value: '1', unit: '元' },
  { label: '万', value: '2', unit: '万' },
  { label: '亿', value: '3', unit: '亿' },
  { label: 'K(1,000)', value: '4', unit: 'K' },
  { label: 'M(1,000,000)', value: '5', unit: 'M' },
  { label: 'B(1,000,000,000)', value: '6', unit: 'B' },
]
export const coinType = ['￥', '$']

/*option: {
  current: '1',  1：数值，2：货币， 3：百分比
  num: 0, 小数点位数
  unit: '', 单位
  split: false, 是否千分位分割
  coinMark:'' 货币符
}*/
export function parse(value, option) {
  if (!option) return value
  if (typeof option === 'string') {
    try {
      option = JSON.parse(option)
    } catch {
      console.error('传入参数错误')
      return value
    }
  }
  if (option.current === '1') {
    value = (value / 1).toFixed(option.num || 0)
    return option.split ? splitNum(value) + option.unit : value + option.unit
  } else if (option.current === '2') {
    if (option.unit) {
      let unit = unitOptions.find((item) => item.value === option.unit)?.unit
      switch (option.unit) {
        case '1':
          value = (value / 1).toFixed(option.num || 0)
          return `${option.coinMark || ''}${option.split ? splitNum(value) : value}${unit}`
        case '2':
          value = (value / 10000).toFixed(option.num || 0)
          return `${option.coinMark || ''}${option.split ? splitNum(value) : value}${unit}`
        case '3':
          value = (value / 100000000).toFixed(option.num || 0)
          return `${option.coinMark || ''}${option.split ? splitNum(value) : value}${unit}`
        case '4':
          value = (value / 1000).toFixed(option.num || 0)
          return `${option.coinMark || ''}${option.split ? splitNum(value) : value}${unit}`
        case '5':
          value = (value / 1000000).toFixed(option.num || 0)
          return `${option.coinMark || ''}${option.split ? splitNum(value) : value}${unit}`
        case '6':
          value = (value / 1000000000).toFixed(option.num || 0)
          return `${option.coinMark || ''}${option.split ? splitNum(value) : value}${unit}`
      }
    } else {
      value = (value / 1).toFixed(option.num || 0)
      return `${option.coinMark || ''}${option.split ? splitNum(value) : value}`
    }
  } else if (option.current === '3') {
    value = (value * 100).toFixed(option.num || 0)
    return `${option.split ? splitNum(value) : value}%`
  }
}

function splitNum(num) {
  num = num + ''
  let arr = num.split('.')
  let newNum = arr[0].split('').reverse()
  let newArr = []
  for (let i = 0; i < newNum.length; i++) {
    if (i % 3 === 0 && i !== 0) {
      newArr.push(',')
    }
    newArr.push(newNum[i])
  }
  return newArr.reverse().join('') + (arr[1] ? `.${arr[1]}` : '')
}
